using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using PMClient = UnityEditor.PackageManager.Client;

namespace Devsowt
{
    public class PackageManager_EditorWindow : EditorWindow
    {
        private string giteeUrl = "https://gitee.com/Devsowt";
        private string gitUrl = "https://github.com";

        private void Init()
        {
            packages = new List<Package>();

            packages.Add(new Package()
            {
                url = SetUrl("com.unity.nuget.newtonsoft-json", PackageType.Public),
                packageName = "com.unity.nuget.newtonsoft-json",
                displayName = "Newtonsoft-json"
            });

            packages.Add(new Package()
            {
                url = SetUrl("Cysharp/UniTask.git?path=src/UniTask/Assets/Plugins/UniTask", PackageType.Git),
                packageName = "com.cysharp.unitask",
                displayName = "UniTask"
            });

            packages.Add(new Package()
            {
                url = SetUrl("astar.git", PackageType.Gitee),
                packageName = "com.arongranberg.astar",
                displayName = "AStar"
            });

            packages.Add(new Package()
            {
                url = SetUrl("trilib.git", PackageType.Gitee),
                packageName = "com.devsowt.trilib",
                displayName = "TriLib"
            });

            packages.Add(new Package()
            {
                url = SetUrl("web-view.git", PackageType.Gitee),
                packageName = "com.devsowt.webview",
                displayName = "WebView"
            });

            packages.Add(new Package()
            {
                url = SetUrl("best-http.git", PackageType.Gitee),
                packageName = "com.tivadar.best.http",
                displayName = "Best HTTP"
            });

            packages.Add(new Package()
            {
                url = SetUrl("uiframework.git", PackageType.Gitee),
                packageName = "com.devsowt.uiframework",
                displayName = "UIFramework"
            });

            packages.Add(new Package()
            {
                url = SetUrl("dotween.git", PackageType.Gitee),
                packageName = "com.devsowt.dotween",
                displayName = "DOTween"
            });

            packages.Add(new Package()
            {
                url = SetUrl("highlight-plus.git", PackageType.Gitee),
                packageName = "com.devsowt.highlight",
                displayName = "HighlightPlus"
            });
        }

        #region Private
        private Dictionary<string, string> unityPackage;
        private Dictionary<string, string> giteePackage;
        private Dictionary<string, string> gitPackege;
        private List<Package> packages;

        private class Package
        {
            public string url;
            public string displayName;
            public string packageName;
            public bool install = false;
            public bool selected = false;
        }

        private enum PackageType
        {
            Public,
            Git,
            Gitee
        }

        private string SetUrl(string baseUrl, PackageType type)
        {
            switch (type)
            {
                case PackageType.Git:
                    return $"{gitUrl}/{baseUrl}";
                case PackageType.Gitee:
                    return $"{giteeUrl}/{baseUrl}";
                case PackageType.Public:
                default:
                    return baseUrl;
            }
        }
        #endregion
        #region Editor
        [MenuItem("Tools/插件管理", priority = -1000)]
        private static void ShowWindow()
        {
            GetWindow(typeof(PackageManager_EditorWindow));
        }

        PackageManager_EditorWindow()
        {
            Init();
            titleContent = new GUIContent("插件管理");
        }

        void OnEnable()
        {
            var list = PMClient.List(true);

            while (!list.IsCompleted) { }

            Package temp = null;
            foreach (var pack in list.Result)
            {
                temp = packages.Find(item => item.packageName == pack.name);
                if (temp != null)
                {
                    temp.install = true;
                    temp.selected = true;
                }
            }
        }

        void OnGUI()
        {
            foreach (var pack in packages)
            {
                pack.selected = EditorGUILayout.Toggle(pack.displayName, pack.selected);
            }

            if (GUILayout.Button("应用"))
            {
                var index = 0;
                var piece = 1f / packages.Count;

                foreach (var pack in packages)
                {
                    EditorUtility.DisplayProgressBar("应用更改", $"正在应用{pack.displayName}...", index++ * piece);
                    if (pack.install != pack.selected)
                    {
                        if (pack.selected)
                        {
                            var request = PMClient.Add(pack.url);
                            while (!request.IsCompleted) { }
                        }
                        else
                        {
                            var request = PMClient.Remove(pack.packageName);
                            while (!request.IsCompleted) { }
                        }
                        pack.install = pack.selected;
                    }
                    EditorUtility.ClearProgressBar();
                }
        
                AssetDatabase.Refresh();
            }
        }
        #endregion
    }
}
